<template>
  <div>
    <h1>当前课程代码</h1>
    <el-table :data="codeData" style="width: 100%">
      <el-table-column prop="name" label="课程名称" width="180"  />
      <el-table-column prop="code" label="课程代码" width="180" />
      <el-table-column prop="operation" label="操作" width="180">
        <template  #default="scope">
          <el-button type="primary" circle @click="editCode(scope.$index)" >
            <i class="el-icon-edit"></i>
          </el-button>
          <el-button type="danger" icon="Delete" circle @click="deleteCode(scope.$index)">
            <i class="el-icon-delete"></i>
          </el-button>
        </template>
      </el-table-column>

    </el-table>

    <el-divider></el-divider>
    <h1>添加课程代码</h1>
    <el-form :model="param" :rules="rules" ref="addCode" label-width="80px">

      <el-form-item label="课程代码" prop="code">
        <el-input type="text" placeholder="输入要添加的课程代码" size="small"
                  prefix-icon="el-icon-info"
                  v-model="param.code" style="width: 200px;"></el-input>
      </el-form-item>
      <el-form-item label="课程名称" prop="name">
        <el-input type="text" placeholder="输入要添加的课程名称" size="small"
                  prefix-icon="el-icon-info"
                  v-model="param.name" style="width: 200px;"></el-input>
      </el-form-item>
      &nbsp;&nbsp;&nbsp;<el-button type="danger" @click="Submit">添加课程代码</el-button>
    </el-form>

    <el-dialog :visible="dialogFormVisible" title="修改课程代码" :close-on-click-modal="false" :append-to-body="true">
      <el-form :model="form">
        <el-form-item label="课程代码" label-width="140px">
          <el-input v-model="form.code" autocomplete="off" />
        </el-form-item>
        <el-form-item label="课程名称" label-width="140px">
          <el-input v-model="form.name" autocomplete="off" />
        </el-form-item>

      </el-form>
      <template #footer>
      <span class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取消</el-button>
        <el-button type="primary" @click="modifyCode">确认修改</el-button>
      </span>
      </template>
    </el-dialog>

  </div>
</template>

<script>
import ElementUI from "element-ui";

export default {
  //报名管理
  name: "AdminManage",
  data(){
    return {
      //是否显示对话框
      dialogFormVisible:false,
      param:{
        code:"",
        name:""
      },
      // 对话框里面的数据
      form:{
        code:"",
        name:""
      },
      index:-1,
      //课程代码
      codeData:[ ],
      //规则
      rules: {
        code: [
          {required: true, message: "请输入要添加的课程代码", trigger: "blur"}
        ],
        name: [
          {required: true, message: "请输入要添加的课程名称", trigger: "blur"}
        ],
      }
    }
  },
  mounted(){
      this.$forceUpdate()         //解决数据渲染延迟
      this.GetCode();
  },
  methods:{
    //获取全部课程代码
    GetCode(){
      //console.log("正在获取课程代码" )
      this.axios.post("showcode"
      ).then((response) => {
        console.log(response.data)
        if (response.data['msg'] == "ok") {
          this.codeData = response.data["course"]
        }
      })
    },
    //修改课程代码
    modifyCode(){
      this.dialogFormVisible = false;
      let index = this.index
      if( this.codeData[index]['code'] ==  this.form.code && this.codeData[index]['name'] ==  this.form.name ){
        //没有什么变化
        this.$message({
          type: 'error',
          message: '修改失败，内容与之前一致'
        });
        return;
      }
      console.log(index+" "+this.codeData[index]['code'])
      this.$confirm('此操作将修改该课程代码及名称, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        let params = new URLSearchParams();
        params.append('oldcode',this.codeData[index]['code']);
        params.append('oldname',this.codeData[index]['name']);
        params.append('newcode',this.form.code);
        params.append('newname',this.form.name);

        this.axios.post("modifycode",
            params
        ).then((response) => {
          console.log(response.data)
          if (response.data['msg'] == "ok") {
            this.$forceUpdate()         //解决数据渲染延迟
            this.GetCode();             //重新获取
          }
        })
        this.$message({
          type: 'success',
          message: '修改成功!'
        });
        this.GetCode();
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消修改'
        });
      });


    },
    //编辑课程代码
    editCode(index){
      this.index = index
      this.$forceUpdate()         //解决数据渲染延迟
      this.dialogFormVisible = true;
      this.form.code = this.codeData[index]['code']
      this.form.name = this.codeData[index]['name']

    },
    //删除课程代码
    deleteCode(index){
      console.log(index+" "+this.codeData[index]['code'])
      this.$confirm('此操作将删除该课程, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        let params = new URLSearchParams();
        params.append('code',this.codeData[index]['code']);
        params.append('name',this.codeData[index]['name']);
        this.axios.post("deletecode",
            params
        ).then((response) => {
          console.log(response.data)
          if (response.data['msg'] == "ok") {
            this.codeData = response.data["course"]
            this.$forceUpdate()         //解决数据渲染延迟
          }
        })
        this.$message({
          type: 'success',
          message: '删除成功!'
        });
        this.GetCode();
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        });
      });

    },
    Submit(){
      this.$confirm('您要添加的课程为：'+this.param.code+"、"+this.param.name+"，是否确认无误", '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        //解决axios提交到后端为null的情况
        let params = new URLSearchParams();
        params.append('code', this.param.code);
        params.append('name', this.param.name);
        console.log(params)
        this.axios.post("addcode",
            params
        ).then((response) => {
          console.log(response.data)
          console.log(response.data['msg'])
          if( response.data['msg'] == "ok"){
            ElementUI.Message({
              message: '成功添加课程代码',
              type: 'success'
            });
            this.GetCode();
          }else {
            ElementUI.Message({
              message: '添加课程代码失败，该课程已存在',
              type: 'error'
            });
          }
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消添加'
        });
      });


    },


  }

}
</script>

<style scoped>

</style>